package com.lgkj.ui.userinfo;

import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.google.gson.Gson;
import com.lgkj.R;
import com.lgkj.bean.CityBean;
import com.lgkj.ui.BaseActivity;
import com.lgkj.utils.CommonConstant;
import com.lgkj.utils.HttpUtils;
import com.lgkj.view.SelectCityDialog;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/8/21 0021.
 */

public class AdressAddActivity extends BaseActivity {
    @BindView(R.id.tv_name)
    EditText tvName;
    @BindView(R.id.tv_phone)
    EditText tvPhone;
    @BindView(R.id.et_adress)
    EditText etAdress;
    @BindView(R.id.cb_moren)
    CheckBox cbMoren;
    @BindView(R.id.et_country)
    EditText etCountry;
    @BindView(R.id.et_city)
    EditText etCity;
    @BindView(R.id.et_xian)
    EditText etXian;
    private CityBean countybean, cityBean, regionBean;
    private CityBean.DataBean countybeanData, cityBeanData, regionBeanData;
    private SelectCityDialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_adress_add;
    }

    @Override
    public void initView() {
        setTitle("添加地址");
        dialog = new SelectCityDialog(AdressAddActivity.this, R.style.city_dialog).setTitle("选择省");
        setOnitemClick();
    }

    @Override
    public void initData() {
        getcounty();
    }


    @OnClick({R.id.et_country, R.id.et_city, R.id.et_xian, R.id.iv_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.et_country:
                if (cityBeanData != null)
                    dialog.setTitles("选择省");
                if (countybean != null && countybean.getData() != null)
                    dialog.setData(countybean.getData(), 0);
                dialog.show();
                break;
            case R.id.et_city:
                if (countybeanData != null)
                    dialog.setTitles(countybeanData.getName());
                if (cityBean != null && cityBean.getData() != null) {
                    dialog.setData(cityBean.getData(), 1);
                    dialog.show();
                } else {
                    toastLong("请选选择省份", 0);
                }

                break;
            case R.id.et_xian:
                if (cityBeanData != null)
                    dialog.setTitles(cityBeanData.getName());
                if (regionBean != null && regionBean.getData() != null) {
                    dialog.setData(regionBean.getData(), 2);
                    dialog.show();
                } else {
                    toastLong("请选选择城市", 0);
                }

                break;
            case R.id.iv_save:
                save();
                break;
        }
    }


    private void save() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        map.put("name", tvName.getText().toString());
        map.put("phone", tvPhone.getText().toString());
        map.put("address", etAdress.getText().toString());
        map.put("is_default", (cbMoren.isChecked() ? 1 : 0) + "");   //1默认   0不默认
        if (countybeanData != null)
            map.put("province_id", countybeanData.getId());
        if (cityBeanData != null)
            map.put("city_id", cityBeanData.getId());
        if (regionBeanData != null)
            map.put("region_id", regionBeanData.getId());
        showDiaLog("正在提交数据");
        HttpUtils.postHttp(map, CommonConstant.addAddress, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                dismissDiaLog();
                if (jsonObject.optInt("code") == 0) {
                    toastLong("地址添加成功", 0);
                    finish();
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {
                dismissDiaLog();
            }
        });
    }

    /**
     *
     */
    private void getcounty() {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        HttpUtils.postHttp(map, CommonConstant.getProvinceAll, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    Gson mGson = new Gson();
                    countybean = mGson.fromJson(String.valueOf(jsonObject), CityBean.class);
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {
            }
        });
    }

    /**
     *
     */
    private void getcity(String province_id) {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        map.put("province_id", province_id);
        HttpUtils.postHttp(map, CommonConstant.getCityByProvince, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    Gson mGson = new Gson();
                    cityBean = mGson.fromJson(String.valueOf(jsonObject), CityBean.class);
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {
            }
        });
    }

    private void getRegion(String city_id) {
        Map<String, String> map = new HashMap<>();
        map.put("access_token", getToken());
        map.put("city_id", city_id);
        HttpUtils.postHttp(map, CommonConstant.getRegionByCity, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                if (jsonObject.optInt("code") == 0) {
                    Gson mGson = new Gson();
                    regionBean = mGson.fromJson(String.valueOf(jsonObject), CityBean.class);
                } else {
                    toastLong(jsonObject.optString("msg"), 0);
                }
            }

            @Override
            public void errorHttp(String msg) {
            }
        });
    }

    private void setOnitemClick() {
        dialog.setResult(new SelectCityDialog.ResultBack() {
            @Override
            public void result(CityBean.DataBean dataBean, int type) {
                Log.i(TAG, "result: " + dataBean.toString());
                if (type == 0) {
                    countybeanData = dataBean;
                    getcity(dataBean.getId());
                    etCountry.setText(dataBean.getName());
                } else if (type == 1) {
                    cityBeanData = dataBean;
                    getRegion(dataBean.getId());
                    etCity.setText(dataBean.getName());
                } else {
                    regionBeanData = dataBean;
                    etXian.setText(dataBean.getName());
                }
                dialog.dismiss();
            }
        });
    }


}
